package chapters18;


import java.util.Random;
import java.util.concurrent.*;

/**
 * Created by xinxuchu on 2020/2/1.
 *
 */
public class BasicDemo {

    //1.要执行的异步服务
    static class Task implements Callable<Integer>{

        @Override
        public Integer call() throws Exception {
            int sleepSeconds = new Random().nextInt(1000);
            Thread.sleep(sleepSeconds);
            return sleepSeconds;
        }
    }

    public static void main(String[] args) throws InterruptedException{
        //2.执行服务
        ExecutorService executor = Executors.newSingleThreadExecutor();
        //3.服务执行的结果
        Future<Integer> future = executor.submit(new Task());
        //模拟执行其他任务
        Thread.sleep(1000);
        try {
            System.out.println(future.get());
        }catch (ExecutionException e){
            e.printStackTrace();
        }
        executor.shutdown();
    }



}
